#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} ERROR ${RESET}${TEXT} You need to run this script as root! ${RESET}"; exit 1
fi

alamat_ip=$(wget -qO- ipv4.icanhazip.com)
alamat_hos=$(cat /etc/environment | grep 'DOMAIN' | cut -d '=' -f 2)
last_port_tls=$( cat /etc/shadowsocks-libev/.accounts | tail -n1 | awk '{print $3}' )
last_port_http=$( cat /etc/shadowsocks-libev/.accounts | tail -n1 | awk '{print $4}' )

read -p "Enter Username" _namapengguna
read -p "Enter password" _katalaluan
read -p "Active duration" _tempohaktif
tarikh_luput=$(date -d "$_tempohaktif days" +"%F")

egrep "^$_namapengguna" /etc/shadowsocks-libev/.accounts &>/dev/null
if [[ $? -eq 0 ]]; then
    echo -e "${ERROR} ERROR ${RESET}${TEXT} Username already exist! ${RESET}"; exit 1
fi
egrep "^$_katalaluan" /etc/shadowsocks-libev/.accounts &>/dev/null
if [[ $? -eq 0 ]]; then
    echo -e "${ERROR} ERROR ${RESET}${TEXT} Invalid password! ${RESET}"; exit 1
fi

if [[ $last_port_tls = '' ]]; then
    port_tls=6101
else
    port_tls="$(( $last_port_tls + 1 ))"
fi
if [[ $last_port_http = '' ]]; then
    port_http=6001
else
    port_http="$(( $last_port_http + 1 ))"
fi

cat > /etc/shadowsocks-libev/$_namapengguna-tls.json<<END
{
    "server":"0.0.0.0",
    "server_port":$port_tls,
    "local_port":1080,
    "password":"$_katalaluan",
    "method":"chacha20-ietf-poly1305",
    "timeout":60,
    "fast_open":true,
    "no_delay":true,
    "plugin":"obfs-server",
    "plugin_opts":"obfs=tls",
    "nameserver":"1.1.1.1",
    "mode":"tcp"
}
END

cat > /etc/shadowsocks-libev/$_namapengguna-http.json <<-END
{
    "server":"0.0.0.0",
    "server_port":$port_http,
    "local_port":1080,
    "password":"$_katalaluan",
    "method":"chacha20-ietf-poly1305",
    "timeout":60,
    "fast_open":true,
    "no_delay":true,
    "plugin":"obfs-server",
    "plugin_opts":"obfs=http",
    "nameserver":"1.1.1.1",
    "mode":"tcp"
}
END

systemctl start shadowsocks-libev-server@$_namapengguna-tls.service &>/dev/null
systemctl enable shadowsocks-libev-server@$_namapengguna-tls.service &>/dev/null
systemctl start shadowsocks-libev-server@$_namapengguna-http.service &>/dev/null
systemctl enable shadowsocks-libev-server@$_namapengguna-http.service &>/dev/null

libev_tls=$( echo -n "chacha20-ietf-poly1305:$_katalaluan@$alamat_hos:$port_tls" | base64 )
libev_http=$( echo -n "chacha20-ietf-poly1305:$_katalaluan@$alamat_hos:$port_http" | base64 )
echo "$_namapengguna $_katalaluan $port_tls $port_http $tarikh_luput" >> /etc/shadowsocks-libev/.accounts

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} SS-LIBEV - CREATE ACCOUNT                                  ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo -e "${YELLOW}  Address:${PLAIN} ${GREEN}$alamat_ip${PLAIN}"
echo -e "${YELLOW} Hostname:${PLAIN} ${GREEN}$alamat_hos${PLAIN}"
echo -e "${YELLOW}    Ports:${PLAIN} ${GREEN}$port_tls(tls) $port_http(http)${PLAIN}"
echo -e "${YELLOW} Username:${PLAIN} ${GREEN}$_namapengguna${PLAIN}"
echo -e "${YELLOW} Password:${PLAIN} ${GREEN}$_katalaluan${PLAIN}"
echo -e "${YELLOW} Duration:${PLAIN} ${GREEN}$_tempohaktif days${PLAIN}"
echo -e "${YELLOW}  Expired:${PLAIN} ${GREEN}$tarikh_luput${PLAIN}"
echo
echo "  TLS File: $libev_tls#$_namapengguna"
echo " HTTP File: $libev_http#$_namapengguna"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
